package tools.clustering.isodata;

public class run_isodata {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception{
		int K0=100,I0=25,P0=10,On0=5,N0=100;
		double Os0=1.5,Oc0=8;
		boolean loadOld=true,save=false;
		String path="",name="",matrixFile="";
		if(args.length==3){
			matrixFile=args[0];
			path=args[1];
			name=args[2];
		}else if(args.length==4){
			matrixFile=args[0];
			path=args[1];
			name=args[2];
			if(args[3].equals("new")){
				loadOld=false;
				save=true;
			}else{
				System.out.println(printHelp());
				System.exit(616);
			}
		}else if(args.length==10){
			matrixFile=args[0];
			path=args[1];
			name=args[2];
			K0=Integer.parseInt(args[3]);
			I0=Integer.parseInt(args[4]);
			P0=Integer.parseInt(args[5]);
			On0=Integer.parseInt(args[6]);
			Os0=Double.parseDouble(args[7]);
			Oc0=Double.parseDouble(args[8]);
			N0=Integer.parseInt(args[9]);
		}else if(args.length==11){
			matrixFile=args[0];
			path=args[1];
			name=args[2];
			K0=Integer.parseInt(args[3]);
			I0=Integer.parseInt(args[4]);
			P0=Integer.parseInt(args[5]);
			On0=Integer.parseInt(args[6]);
			Os0=Double.parseDouble(args[7]);
			Oc0=Double.parseDouble(args[8]);
			N0=Integer.parseInt(args[9]);
			if(args[10].equals("new")){
				loadOld=false;
				save=true;
			}else{
				System.out.println(printHelp());
				System.exit(616);
			}
		} else{
			System.out.println(printHelp());
			System.exit(616);
		}
		System.out.println("clustering");
		for(int i=0;i<N0;i++){
			isodata id=new isodata(matrixFile,loadOld);
			if(save){
				id.saveDatumToFile(path+name+"_datum.object");
				save=false;
				loadOld=true;
				matrixFile=path+name+"_datum.object";
			}
			id.cluster(K0,I0,P0,On0,Os0,Oc0);
			id.printResult(path+"results/"+name+"_"+i,path+name,true);
		}
		System.out.println("Finding intersection...");
		isodata_stat.intersectionClusters(path+name+"_many.csv", path+name+"_intersection");
		isodata_stat.consensusClusters(path+name+"_intersection.csv", path+name, 0.75*N0);

		
		System.out.println("Done!");
		System.exit(0);
	}

	public static String printHelp(){
		String help="isodata clustering input alternatives:\n";
		help+="\n";
		help+="isodata <matrixFile> <path> <name>\n";
		help+="isodata <matrixFile> <path> <name> <new/save>\n";
		help+="isodata <matrixFile> <path> <name> <K> <I> <P> <On> <Os> <Oc> <N>\n";
		help+="isodata <matrixFile> <path> <name> <K> <I> <P> <On> <Os> <Oc> <N> <new/save>\n";
		help+="\n";
		help+="-matrixFile contains datum vectors\n";
		help+="-path is the path to where the result should be written\n";
		help+="-name is the name of the project\n";
		help+="-new is added at the end if the matrix should be loaded from a text file instead from a binary which is created\n";
		help+="-K= desired number of clusters\n";
		help+="-I= maximum number of iterations\n";
		help+="-P= maximum number of pairs of clusters which can be merged\n";
		help+="-On= minimum number of samples in a cluster\n";
		help+="-Os= threshold for maximum value for std (split)\n";
		help+="-Oc= threshold for minimum distance (merge)\n";
		help+="-N= number of times to cluster the data\n";
		return help;
	}
	/*
	 * K= desired number of clusters
	 * I= maximum number of iterations
	 * P= maximum number of pairs of clusters which can be merged
	 * On= minimum number of samples in a cluster
	 * Os= threshold for maximum value for std (split)
	 * Oc= threshold for minimum distance (merge)
	 */
}
